<template>
  <div class="song-list-detail">
    <div class="song-list-detail_cover">
      <img :src="`${songListDetal?.coverImgUrl}?param=200y200`" alt="" />
      <van-icon name="arrow-left" class="icon" @click="$router.back()"></van-icon>
      <div class="song-list-detail_cover_content">
        <div class="song-list_img">
          <img :src="`${songListDetal?.coverImgUrl}?param=200y200`" alt="" />
          <div class="song-list_img_count">
            <van-icon name="audio"></van-icon>
            {{ formatCount(songListDetal?.playCount) }}
          </div>
        </div>
        <div class="song-list_info">
          <b>{{ songListDetal?.name }}</b>
          <p>{{ songListDetal?.creator.nickname }}</p>
          <p style="font-size: 12px">{{ songListDetal?.creator.description || "简简单单，没有简介" }}</p>
        </div>
      </div>
    </div>
    <div class="song-list-detail_contain">
      <div class="contain_head">
        <van-icon name="play-circle-o"></van-icon>
        播放全部
        <span style="font-size: 18px; color: var(--font-light-color)">共({{ musicState.musicIds.length }}首)</span>
      </div>
      <van-list
        v-model:loading="musicState.loading"
        :finished="musicState.finished"
        :immediate-check="false"
        @load="handleMusicLoad"
      >
        <fs-song-item
          :order="index + 1"
          color="var(--font-color)"
          light-color="var(--font-light-color)"
          :is-search="false"
          v-for="(item, index) in musicState.musicList"
          :music-detail="item"
          :key="index"
        />
      </van-list>
    </div>
  </div>
</template>

<script setup lang="ts">
import { onActivated, ref, reactive } from "vue";
import { useRoute } from "vue-router";
import FsSongItem from "@/components/FsSongItem/FsSongItem.vue";
import { getPlayListDetail, getMusicDetailByListId } from "@/service/api/musicRequest";
import type { ISearchSongItem } from "@/types/music/searchSongType";
import type { IPlayListDetail } from "@/types/music/playListType";
import { formatCount } from "@/utils/handleCount";

const $route = useRoute();

const songListDetal = ref<IPlayListDetail>();

const musicState = reactive({
  loading: false,
  finished: false,
  offset: 0,
  limit: 20,
  musicIds: [] as number[],
  musicList: [] as ISearchSongItem[],
});
onActivated(() => {
  getPlayListDetail(Number($route.params.id)).then((res) => {
    songListDetal.value = res.playlist;
    musicState.musicIds = res.playlist.trackIds.map((item) => item.id);
    getMusicListData().then((res) => {
      musicState.musicList.push(...res.songs);
      musicState.offset = 20;
    });
  });
});

const handleMusicLoad = () => {
  getMusicListData(musicState.offset, musicState.limit).then((res) => {
    musicState.musicList.push(...res.songs);
    musicState.loading = false;
    if (musicState.musicList.length >= musicState.musicIds.length) {
      musicState.finished = true;
    } else {
      musicState.offset += musicState.limit;
    }
  });
};

const getMusicListData = (offset = 0, limit = 20) => {
  const ids = musicState.musicIds.slice(offset, limit + offset).join(",");
  return getMusicDetailByListId(ids);
};
</script>

<style scoped lang="scss">
.song-list-detail {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  overflow-x: hidden;
  overflow-y: scroll;
  z-index: 1003;
  background: var(--box-color);
  color: var(--font-color);

  &_cover {
    position: relative;
    width: 100%;
    height: 45vh;
    .icon {
      position: absolute;
      top: 20px;
      left: 10px;
      color: #fff;
      font-size: 20px;
      z-index: 2;
    }
    img {
      width: 100%;
      height: 100%;
      object-fit: cover;
      filter: blur(10px);
    }
    &_content {
      position: absolute;
      display: flex;
      align-items: center;
      bottom: 100px;
      left: 20px;
      color: #fff;
      z-index: 2;
      .song-list_img {
        position: relative;
        width: 130px;
        height: 130px;
        margin-right: 20px;
        img {
          filter: none;
          border-radius: 10px;
        }
        &_count {
          position: absolute;
          bottom: 5px;
          right: 5px;
          font-size: 14px;
        }

        &::after {
          position: absolute;
          content: "";
          top: 50%;
          transform: translateY(-50%) scale(0.9);
          right: -15px;
          width: 100%;
          height: 100%;
          border-radius: 10px;
          background-color: #aaa;
          z-index: -1;
        }
      }
      .song-list_info {
        flex: 1;
        display: flex;
        height: 100px;
        flex-direction: column;
        justify-content: space-between;
        b {
          font-size: 20px;
        }
        p {
          margin: 0;
        }
      }
    }
    &:after {
      position: absolute;
      content: "";
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      background-color: rgba(0, 0, 0, 0.2);
      z-index: 1;
    }
  }
  &_contain {
    position: absolute;
    width: 100%;
    height: 60vh;
    background-color: var(--box-color);
    top: 40vh;
    border-top-left-radius: 30px;
    border-top-right-radius: 30px;
    z-index: 1;
    .contain_head {
      padding: 20px 20px 10px 20px;
      font-size: 23px;
    }
  }
}
</style>
